﻿//Copyright (C) Riwut Libinuko, 2010 - Ideas For Free
//http://blog.libinuko.com
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SharePointZip.Controller;
using System.IO;

namespace SharePointZip.Base
{
    /// <summary>
    /// Base implementation of <see cref="ICompressorEngine&lt;TEngine&gt;"/>.
    /// The compressor engine, must be type of <see cref="IDisposable"/>.
    /// </summary>
    /// <typeparam name="TEngine">Compressor engine type</typeparam>
    public abstract class DeflaterBase<TEngine> : 
            IDisposable ,
            ICompressorEngine<TEngine>
        where TEngine : IDisposable
    {
        #region Properties
        private bool disposed = false;
        protected TEngine zipEngine = default(TEngine);
        protected TEngine ZipEngine
        {
            get { return zipEngine; }
        }
        #endregion

        #region ctor
        protected DeflaterBase()
        {
            
        }
        #endregion

        #region Disposable
        public void Close()
        {
            this.Dispose();
        }
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
        protected virtual void Dispose(bool disposing)
        {
            if (!disposed)
            {
                if (disposing)
                {
                    if (ZipEngine != null)
                        ZipEngine.Dispose();
                }
            }
            disposed = true;
        }
        #endregion

        #region ICompressorEngine<TEngine> member
        /// <summary>
        /// Start compressing type T using TEngine
        /// </summary>
        /// <typeparam name="T">Type of element to be archvied.</typeparam>
        /// <param name="objSrc">Input of type T</param>
        public virtual void StartCompressing<T>(T objSrc)             
        {
            var compressor = ServiceLocator.Instance.GetService<ICompress<TEngine, T>>();
            compressor.CreateCompressedStream(ZipEngine, objSrc);    
          
        }
        /// <summary>
        /// Start compressing of type T to the stream.
        /// </summary>
        /// <typeparam name="T">Type of element to be archvied.</typeparam>
        /// <param name="objSrc">Input of type T</param>
        /// <returns><see cref="Stream"/> object</returns>
        public abstract Stream StartCompressingStream<T>(T objSrc);

        #endregion

    }
}
